<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE html>
<html id="iframe">
	<head>
		<meta charset="UTF-8">
		
		<title><?php echo C('sitename');?></title>
		<meta name="keywords" content="<?php echo C('keyword');?>">
		<meta name="description" content="<?php echo C('content');?>">
		
		<meta name="renderer" content="webkit">
		<meta name="format-detection" content="telephone=no,email=no"/>
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
		<meta name="telephone=no"  content="format-detection">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="format-detection" content="telephone=no" />
		
		<META HTTP-EQUIV="Pragma" CONTENT="no-cache"> 
		<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache"> 
		<META HTTP-EQUIV="Expires" CONTENT="0">
		
		<link rel="stylesheet" type="text/css" href="/Public/Home/css/lib.css"/>
		<link rel="stylesheet" type="text/css" href="/Public/Home/css/public.css"/>
		<link rel="stylesheet" type="text/css" href="/Public/Home/css/index.css" />
		<link rel="stylesheet" type="text/css" href="/Public/Home/css/page.css"/>
		
	</head>
	<style>
		.content p{
			display: inline-block;
		}
	</style>
	<body onload="connect();">
		<div class="main-box">
			<!--第一行（表单、积分在线人数）-->
			<div class="top">
				<input id="textarea" type="text" required/>
				<button onclick="onSubmit(); return false;">发送</button>
				<div class="info">
					<p>剩余点数：<span id="sy"><?php echo ($userinfo["points"]); ?></span>点</p>
					<p>线上人数：<span id="xs"></span>人</p>
				</div>
			</div>
		</div>
		<div id="load" style="text-align: center;font-size: 14px;">loading...</div>
		<div class="chatting" style="display: none;">
			<?php if(is_array($list)): $i = 0; $__LIST__ = $list;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$vo): $mod = ($i % 2 );++$i; if($vo['type'] == 'admin'): ?><div <?php if($vo["uid"] == $userinfo['id']): ?>class="chatting-li qtr green"<?php else: ?>class="chatting-li qtr orange"<?php endif; ?> >
						<div class="portrait"><img src="<?php echo ($vo["head_img_url"]); ?>"/></div>
						<div class="content">
							<small><span class="name"><?php echo ($vo["from_client_name"]); ?></span><time><?php echo ($vo["time"]); ?></time></small>
							<p><?php echo (stripcslashes(htmlspecialchars_decode($vo["content"]))); ?></p>
						</div>
					</div><?php endif; ?>
				<?php if($vo['type'] == 'system'): ?><div class="chatting-li qtr pink">
						<div class="portrait"><img src="<?php echo ($vo["head_img_url"]); ?>"/></div>
						<div class="content">
							<small><span class="name"><?php echo ($vo["from_client_name"]); ?></span><time><?php echo ($vo["time"]); ?></time></small>
							<p><?php echo (stripcslashes(htmlspecialchars_decode($vo["content"]))); ?>。</p>
						</div>
					</div><?php endif; ?>
				<?php if($vo['type'] == 'say'): ?><div <?php if($vo["uid"] == $userinfo['id']): ?>class="chatting-li green"<?php else: ?>class="chatting-li"<?php endif; ?> >
						<div class="portrait"><img src="<?php echo ($vo["head_img_url"]); ?>"/></div>
						<div class="content">
							<small><span class="name"><?php echo ($vo["from_client_name"]); ?></span><time><?php echo ($vo["time"]); ?></time></small>
							<p><?php echo (stripcslashes(htmlspecialchars_decode($vo["content"]))); ?></p>
						</div>
					</div><?php endif; ?>
				<?php if($vo['type'] == 'error' && $userinfo['id'] == $vo['uid']): ?><div class="chatting-li qtr green">
						<div class="portrait"><img src="<?php echo ($vo["head_img_url"]); ?>"/></div>
						<div class="content">
							<small><span class="name"><?php echo ($vo["from_client_name"]); ?></span><time><?php echo ($vo["time"]); ?></time></small>
							<p><?php echo (stripcslashes(htmlspecialchars_decode($vo["content"]))); ?></p>
						</div>
					</div><?php endif; ?>
				<?php if($vo['type'] == 'say_error' && $userinfo['id'] == $vo['uid']): ?><div class="chatting-li green">
						<div class="portrait"><img src="<?php echo ($vo["head_img_url"]); ?>"/></div>
						<div class="content">
							<small><span class="name"><?php echo ($vo["from_client_name"]); ?></span><time><?php echo ($vo["time"]); ?></time></small>
							<p><?php echo (stripcslashes(htmlspecialchars_decode($vo["content"]))); ?></p>
						</div>
					</div><?php endif; endforeach; endif; else: echo "" ;endif; ?>
		</div>
		
		<script src='//cdn.bootcss.com/jquery/1.11.3/jquery.js'></script>
		<!--聊天-->
		<script type="text/javascript" src="/Public/worker/js/swfobject.js"></script>
		<script type="text/javascript" src="/Public/worker/js/web_socket.js"></script>
		<script src="/Public/layer/layer.js"></script>
		<script type="text/javascript">
			setTimeout(function() { 
				$('#load').css('display','none');
				$('.chatting').css('display','block');
			}, 1500); 
			
			document.onkeydown=function(event){
               e = event ? event :(window.event ? window.event : null);
               if(e.keyCode==13){
                    onSubmit(); return false;
               }
            }
			
			if(typeof console == "undefined") {
				this.console = {
					log: function(msg) {}
				};
			}
		/*	WEB_SOCKET_SWF_LOCATION = "/Public/worker/swf/WebSocketMain.swf";
			WEB_SOCKET_DEBUG = true;*/
			var ws, name;

			// 连接服务端
			function connect(){
				// 创建websocket
				ws = new WebSocket("ws://" + document.domain + ":7272");
				// 当socket连接打开时，发送登录信息
				ws.onopen = function(){

					var name = "覃";
					// 登录
					var userid = 1;
					var login_data = '{"type":"login","client_name":"' + name.replace(/"/g, '\"') + '","client_id":"'+userid+'"}';
					console.log("websocket握手成功，发送登录数据:" + login_data);
					ws.send(login_data);
				};
				// 当有消息时根据消息类型显示不同信息
				ws.onmessage = onmessage;
				ws.onclose = function(){
					console.log("连接关闭，定时重连");
					// connect();
				};
				ws.onerror = function() {
					console.log("出现错误");
				};
			}

			// 服务端发来消息时
			var inte = parseInt(Math.random()*12+1);
			function onmessage(e) {
				//console.log(e.data);
				var data = eval("(" + e.data + ")");
				switch(data['type']) {
					// 服务端ping客户端
					case 'ping':
						$('#xs').html(data.content+<?php echo C('online');?>);
						ws.send('{"type":"pong"}');
						inte--;
						if(inte==0){
							ws.send('{"type":"robot"}');
							inte = parseInt(Math.random()*15+1)+3;
						}
						//console.log(inte);
						break;;
						// 登录 更新用户列表
					case 'login':
						console.log(data['client_name'] + "登录成功");
						break;
						// 发言
					case 'say':
						say(data['uid'],data['from_client_name'], data['head_img_url'], data['content'], data['time']);
						break;
						// 用户退出 更新用户列表
					case 'logout':
						break;
					case 'broadcast':
						//alert('client');
					
						//房管
					case 'admin':
						if('<?php echo ($userinfo["id"]); ?>'==data['uid']){
							$(".chatting").prepend('<div class="chatting-li qtr green"><div class="portrait"><img src="'+data['head_img_url']+'"/></div><div class="content"><small><span class="name">' + data["from_client_name"] +'</span><time>'+data["time"] +'</time></small><p>' + data["content"] + '</p></div></div>');
						}else{
							$(".chatting").prepend('<div class="chatting-li qtr orange"><div class="portrait"><img src="'+data['head_img_url']+'"/></div><div class="content"><small><span class="name">' + data["from_client_name"] +'</span><time>'+data["time"] +'</time></small><p>' + data["content"] + '</p></div></div>');
						}
						break;
						//系统
					case 'system':
						if('<?php echo ($userinfo["id"]); ?>'==data['uid']){
							$(".chatting").prepend('<div class="chatting-li qtr green"><div class="portrait"><img src="'+data['head_img_url']+'"/></div><div class="content"><small><span class="name">' + data["from_client_name"] +'</span><time>'+data["time"] +'</time></small><p>' + data["content"] + '</p></div></div>');
						}else{
							$(".chatting").prepend('<div class="chatting-li qtr orange"><div class="portrait"><img src="'+data['head_img_url']+'"/></div><div class="content"><small><span class="name">' + data["from_client_name"] +'</span><time>'+data["time"] +'</time></small><p>' + data["content"] + '</p></div></div>');
						}
						break;
						//积分减
					case 'points':
						$('#sy').html((parseFloat($('#sy').html())-data['content']).toFixed(1));
						break;
						//积分加
					case 'pointsadd':
						$('#sy').html((parseFloat($('#sy').html())+data['points']).toFixed(1));
						parent.layer.msg('恭喜竞猜成功');
						break;
						//重载
					case 'reload':
						if('<?php echo ($userinfo["id"]); ?>'==9){
							window.location.href=window.location.href;
						}
						break;
						//切换
					case 'switch':
						parent.location.reload();
						break;	
				}
			}
			
			
			// 提交对话
			function onSubmit() {
				var headimgurl = '<?php echo ($userinfo["headimgurl"]); ?>';
				var input = document.getElementById("textarea");
				var from_client_name = '<?php echo ($userinfo["nickname"]); ?>';
				if(input.value==''){
					$('#textarea').focus();
					return false;
				}
				ws.send('{"type":"say","client_name":"'+from_client_name+'","headimgurl":"'+headimgurl+'","content":"' + input.value.replace(/"/g, '\"').replace(/n/g, '\n').replace(/r/g, '\r') + '"}');
				$('#textarea').val('').focus();
				//$('#dialog').scrollTop(0);
			}
			// 发言
			function say(uid, from_client_name, head_img_url, content, time) {
				if('<?php echo ($userinfo["id"]); ?>'==uid){
					$(".chatting").prepend('<div class="chatting-li green"><div class="portrait"><img src="'+head_img_url+'"/></div><div class="content"><small><span class="name">' + from_client_name +'</span><time>'+time +'</time></small><p>' + content + '</p></div></div>');
				}else{
					$(".chatting").prepend('<div class="chatting-li"><div class="portrait"><img src="'+head_img_url+'"/></div><div class="content"><small><span class="name">' + from_client_name +'</span><time>'+time +'</time></small><p>' + content + '</p></div></div>');
				}
			}
		</script>
		<!--聊天-->
		
		<!--推送-->
		<script src='//cdn.bootcss.com/socket.io/1.3.7/socket.io.js'></script>
		<script>
			var uid = '<?php echo ($userinfo["id"]); ?>';
		</script>
		<script>
			function escape2Html(str) {
			 	var arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'};
			 	return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];});
			}
			$(document).ready(function() {
				// 连接服务端
				var socket = io('http://' + document.domain + ':2120');
				// 连接后登录
				socket.on('connect', function() {
					socket.emit('login', uid);
				});
				// 后端推送来消息时
				socket.on('new_msg', function(msg) {
					var data = eval("(" + escape2Html(msg) + ")");
					console.log(escape2Html(msg));
					var time = data['time'];
					var content = data['content'];
					if(data['points'] && data['to']=='<?php echo ($userinfo["id"]); ?>'){
						var points = parseFloat($('#sy').html())+parseFloat(data['points']);
						$('#sy').html(points);
					}
					$(".chatting").prepend('<div class="chatting-li qtr pink"><div class="portrait"><img src="/Public/main/img/system.jpg"/></div><div class="content"><small><span class="name">客服</span><time>'+time+'</time></small><p>' + content + '</p></div></div>');
				});
			});
		</script>
		<!--推送-->
	</body>
</html>